package com.app.configuration.web.mvc;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.app.util.common.PathUtil;

/**
 * 文件路径配置，将服务器硬盘物理路径映射到项目中相对路径
 * @author chenl
 *
 */
@Configuration
public class WebMvcResourceHandlersConfiguration  implements WebMvcConfigurer {
	@Value("${sysConfig.upload_realPath}")
	private String upload_realPath;		//文件存放的硬盘实际位置,格式如:D:/twoa2019/
	@Value("${sysConfig.PATH_COMM_FILE}")
	private String PATH_COMM_FILE;		//附件上传的相对位置,格式如：/upload
	/**
	 * 将服务器硬盘物理路径映射到项目中相对路径
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//文件磁盘图片url 映射
		//配置server虚拟路径，addResourceHandler为前台访问的目录，addResourceLocations为files相对应的本地路径
		//如：  /aa/** 实际存放位置“file:d:/file/aa/”则表示  /aa/**下所有的访问文件，实际对应到d:/file/aa下
		if(PATH_COMM_FILE != null && PATH_COMM_FILE.endsWith("/")) {
			PATH_COMM_FILE = PATH_COMM_FILE.substring(0, PATH_COMM_FILE.length()-1);
		}
		System.out.print(PATH_COMM_FILE+"------"+PathUtil.getClasspath());
		registry.addResourceHandler(PATH_COMM_FILE+"/**").addResourceLocations("file:/"+PathUtil.getClasspath()+PATH_COMM_FILE+"/");
//		System.out.println("---------------f"+"file://"+PathUtil.getClasspath()+PATH_COMM_FILE+"/");
//        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
//        // 解决静态资源无法访问
//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
//        registry.addResourceHandler("/swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//        // 解决swagger的js文件无法访问
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");

		//registry.addResourceHandler("/res/**","/res/**");
	}
}
